home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1996 #15 / Monster Media Number 15 (Monster Media)(July 1996).ISO / os2 / srefv112.zip / SRCHINDX.80 < prev    next >
Text File  |  1996-05-25  |  13KB  |  458 lines

  1. /* SRE-FILTER procedure (5/96) to access a "SWISH" index file.
  2.   Requires that a SWISH index have been previously built
  3.   using SWISH.EXE (available from:
  4.        ftp://ftp.eit.com/pub/web.software/swish/
  5.  
  6. (uses some code from DOSEARC.CMD by 3/20/1996, Kevin Vigor)
  7.  
  8. In addition, will attempt to make a short description (if desired),
  9. using either a name= (or http-equiv=) "description" header element,
  10. or the first several <hx>  ... </hx> elements.
  11.  
  12.  
  13. Options:
  14. keyword: List of words to search for, with OR AND NOT as logical
  15.          controls (and is assumed). Note, there is NO phrase support!
  16. index:   Index file to search for (typically provided as type=hidden)
  17. option1..option9: list of options.
  18.                   Valid ones include
  19.                     -t HBethc
  20.                     -m #lines
  21. header:  H1 header to use (default used if none provided )
  22. comment1..commentx: Comments to place (using <EM>) under header
  23. conditon: (=YES,AND,NOT) Or,and,not  substituted between keywords. 
  24.      Default is "and" searches.
  25.     (no substitution occurs if NOT, OR or AND is between words in keyword list)
  26.     This will not work well with (phrases) or in combination with
  27.     complex user specified search strings.
  28.  
  29. summary = NO  YES CREATE
  30.         
  31.         no  =  no summary, but check for existence
  32.         yes = look for name="description" (or http-equiv=.)
  33.         and use the "contents= field)
  34.         create = If no name=found, then use the first several
  35.         <h1, h2, h3 elements.
  36.  
  37.         Otherwise (default) -- no summary, no checking
  38. */
  39.  
  40. swish_index_search:
  41.  
  42. parse arg  ddir, tempfile, reqstrg,list,verb ,uri,user, servdir , ,
  43.                  tempdir,privset,inistuff
  44.  
  45.  
  46. parse var inistuff macrospace_input "," infiles
  47. parse var infiles  a1 ',' a2 ',' virtual_file ',' .
  48.  
  49. call lineout tempfile, '<!doctype html public "-//IETF//DTD HTML 2.0//EN">'
  50. call lineout tempfile, "<html><head><title>Index search results /title>"
  51. call lineout tempfile, "</head><body>"
  52.  
  53.  
  54. /* Begin the result body.*/
  55. tempdir=strip(translate(tempdir,'\','/'),'t','\')
  56. TEMPOUT = dostempName(TEMPDIR'\ST$?????.OUT')
  57.  
  58. if tempout = "0" | tempfile="" then do
  59.      call lineout tempfile," <STRONG> ERROR: Could not access working directory </STRONG>"
  60.      call lineout tempfile," </BODY> </HTML> "
  61.      return 'FILE ERASE TYPE text/html NAME' tempfile
  62. end
  63.  
  64.  
  65. keywords='help'
  66. index_file="INDEX.SWI"
  67. swopts=' '
  68. aheader="Search the site-index "
  69. ncmt=0 ; door=0
  70. summary=1
  71. do until list="" 
  72.  
  73.  
  74.   parse var list v1 '&' list
  75.  
  76.   parse var v1 avar '=' aval ; avar=translate(avar)
  77.   if abbrev(avar,"KEYWORD")=1 then  do
  78.       keywords=packur(translate(aval,' ','+'))
  79.   end
  80.   if abbrev(avar,'HEADER')=1 then do
  81.       aheader=packur(translate(aval,' ','+'))
  82.   end
  83.   if abbrev(avar,'INDEX')=1 then do
  84.      indxfile=aval
  85.   end
  86.   if abbrev(avar,'COMMENT')=1 then do
  87.       ncmt=ncmt+1
  88.       comments.ncmt=translate(aval, ' ','+'||'00090d0a'x) 
  89.   end
  90.   if abbrev(avar,'COND')=1 then do
  91.         select
  92.          when abbrev(translate(aval),'Y')=1 then
  93.                door=' OR '
  94.          when abbrev(translate(aval),'O')=1 then
  95.                door=' OR '
  96.           when abbrev(translate(aval),'N')=1 then
  97.                 door=' NOT '
  98.          otherwise
  99.                 door=0
  100.         end
  101.   end
  102.  
  103.   if abbrev(avar,'SUMMARY')=1 then do
  104.         tt=translate(aval)
  105.         summary=0
  106.         if tt="NO" then
  107.            summary=1
  108.         if tt="YES" then
  109.            summary=2
  110.         if tt="CREATE" then
  111.            summary=3
  112.   end
  113.  
  114.   if abbrev(avar,'OPTION')=1 then do
  115.       swopts=swopts||" "||translate(aval,' ','+')
  116.   end
  117. end
  118.  
  119. call lineout tempfile,' <h1> ' aheader ' </h1> '
  120. do mm=1 to ncmt
  121.   call lineout tempfile,' <em> ' comments.mm ' </em> <br>'
  122. end
  123.  
  124. twords=translate(keywords) ; srchwords=""
  125. /* remove silly srchwords */
  126. do mm=1 to words(twords)
  127.    aword=word(twords,mm)
  128.    if wordpos(aword,'AND NOT OR')>0 then iterate
  129.    srchwords=srchwords||" "||aword
  130. end
  131.  
  132.  
  133. if door<>0 & words(keywords)>1 then do   /* insert not / or  into keyword list */
  134.    tmp=word(keywords,1) ; wasand=0
  135.    do mmm=2 to words(keywords)
  136.       aww=word(keywords,mmm) ; taw=strip(translate(aww))
  137.       if wasand=1 then do
  138.         tmp=tmp||' '||aww
  139.         wasand=0
  140.         iterate
  141.       end
  142.       if taw="OR"| taw="AND" | taw="NOT"then do
  143.         tmp=tmp||' '||aww
  144.         wasand=1
  145.         iterate
  146.       end
  147.       tmp=tmp||door||aww
  148.     end
  149.     keywords=tmp
  150. end
  151.  
  152. t1='swish -f '||INDXfile ||' -w '||keywords||' '||swopts||' > '||tempout
  153.  
  154. foodir=directory(servdir)
  155. address cmd
  156. t1
  157. address
  158. foodir=directory(foodir)
  159.  
  160. /* get results */
  161. gotem=fileread(tempout,'filelines',,'e')  
  162. goo=sysfiledelete(tempout)
  163.  
  164.  
  165. foo=filelines.gotem
  166. /* --- sample to test remote url retrieval
  167. filelines.gotem='100 http://www.yahoo.com "THis is yahoo " 1 '
  168. gotem=gotem+1
  169. filelines.gotem=foo
  170. ---------- */
  171.  
  172. if gotem=0 then do
  173.    call lineout tempfile," <STRONG> ERROR: Problem searching index </STRONG>"
  174.     call lineout tempfile," </BODY> </HTML> "
  175.    return 'FILE ERASE TYPE text/html NAME' tempfile
  176. end
  177. igot=0
  178.  
  179. do mm=1 to gotem
  180.    RLINE = filelines.mm
  181.  
  182.    IF RLINE = '' THEN leave
  183.    IF RLINE = '.' THEN leave
  184.  
  185.  
  186. /* Skip comment lines and empty lines. */
  187.    IF abbrev(RLINE,'#') = 1  THEN iterate
  188.  
  189. /* Split the line into fields. */
  190.     PARSE VAR RLINE R_SCORE R_FILE R_stuff
  191.  
  192.     foo1=words(r_stuff)
  193.     r_title=delword(r_stuff,foo1)
  194.     r_position=word(r_stuff,foo1)
  195.  
  196. /* Make sure this looks valid (i.e. that position and score are both 
  197.    numeric variables).                                               */
  198.  
  199.   IF VERIFY(R_SCORE, '1234567890') <> 0 | VERIFY(R_POSITION, '1234567890') <> 0 THEN  do
  200.        aa=strip(rline)
  201.        if abbrev(translate(aa),'SEARCH WORDS:') then do
  202.             call lineout tempfile,'<p><strong> Searching for: </strong> '
  203.             parse var rline foo ':' lookme
  204.             call lineout tempfile,' <code> ' lookme '</code>'
  205.        end
  206.        iterate
  207.   end
  208.   igot=igot+1
  209.   if igot=1 then do
  210.         call lineout tempfile,'<h3> Results of search </h3>  <dl> '
  211.    end
  212.  
  213. /* Spit out this reference as a HTML link. */
  214.   r_file=translate(r_file,'/','\')
  215.   r_title=strip(strip(r_title),,'"')
  216.   call lineout tempfile,'<dt> <A href=' r_file '>' r_title '</a>'
  217.   call lineout tempfile,'  <em> Score= ' r_score ' </em> '
  218.   call lineout tempfile,' <code> , ' r_position 'bytes </code> <br>'
  219.   if summary>0 then do
  220.         foo=make_summary(r_file,summary,srchwords)
  221.   end
  222.  
  223. end
  224. if igot>0 then
  225.   call lineout tempfile,'</dl>'
  226.  
  227. if gotit>0 then
  228.   call lineout tempfile,"<p> <em> Total of " igot " matches </em>"
  229.  
  230. call lineout tempfile,'</body></html>'
  231.  
  232. return 'FILE ERASE TYPE text/html NAME' tempfile
  233.  
  234.  
  235. /* ------------------------------------- */
  236. /* write summary info */
  237. make_summary:procedure expose tempfile ddir macrospace_input virtual_file
  238.  
  239. parse arg aurl,asummary, srchwords
  240.  
  241. atype=translate(sref_mediatype(aurl))  /* get mime type */
  242.  
  243. /* strip out http://a.b.c/ */
  244. ishttp=abbrev(strip(translate(aurl)),"HTTP://")
  245. if ishttp=1 then do 
  246.   filename=aurl
  247.    atype="TEXT/HTML"
  248. end
  249.  
  250. if ishttp=0 then do
  251.   afilenam=sref_do_virtual(ddir,aurl,macrospace_input,virtual_file)
  252.   if afilenam=0 then do
  253.      call lineout tempfile,'<dd> <code> Rmote URL not available </code> '
  254.      return 0
  255.   end
  256.   eek=sysfiletree(afilenam,'aflist','F')   /* check for existence*/
  257.   if eek<>0 | aflist.0=0 then do            /* error */
  258.       call lineout tempfile,'<dd> <code> File not available </code> '
  259.       return 0
  260.   end
  261.  
  262.   if asummary=1 then do  /* file exist,return a blank line */
  263.      call lineout tempfile,'<dd>  '
  264.      return 0
  265.   end
  266.  
  267. end
  268.  
  269.  
  270. /*  If  text/plain, return first 200 characters */
  271.  
  272. if atype="TEXT/PLAIN" then do
  273.    if asummary=2 then do
  274.       call lineout tempfile,'<dd> <code> Summary not available </code> '
  275.       return 0
  276.    end
  277.  
  278.  /* else, asummary must be 3 */
  279.     if ishttp=1 then do
  280.         stuff=Sref_get_url(aurl,10000)
  281.         if stuff=0 then do
  282.            call lineout tempfile,'<dd> <code> Summary not available </code> '
  283.            return 0
  284.         end
  285.     end
  286.     else do
  287.        filename=aflist.1
  288.        filename=strip(word(aflist.1,words(aflist.1)))
  289.        alen=min(chars(filename),300)
  290.        stuff=charin(filename,1,alen)
  291.     end
  292.  
  293.     wow=space(translate(stuff,' ','00090a0d1a1b'x))
  294.     wow=sref_replacestrg(wow,'<','<','ALL')
  295.     wow=sref_replacestrg(wow,'>','>','ALL')
  296.     do jmm=1 to words(srchwords)
  297.          aword=strip(word(srchwords,jmm))
  298.          wow=sref_make_block(aword,wow,'<b>','</b>')   /* highlight matches */
  299.      end
  300.     call lineout tempfile,'<dd> (beginning)  ' wow
  301.     return 0
  302. end
  303.  
  304. /* if not html, return no summary*/
  305. if atype<>"TEXT/HTML"  then do
  306.    call lineout tempfile,'<dd> <code> Summary not available </code> '
  307.    return 0
  308. end
  309.  
  310.  
  311. /* if here--  html, summary > 1 */
  312. /* and the url points to a legit file; read it in (up to 10000 characters */
  313.  
  314. if ishttp=1 then do
  315.         stuff=sref_get_url(aurl,10000)
  316.         if stuff=0 then do
  317.            call lineout tempfile,'<dd> <code> Summary not available </code> '
  318.            return 0
  319.         end
  320. end
  321. else do
  322.  
  323.    filename=aflist.1
  324.    filename=strip(word(aflist.1,words(aflist.1)))
  325.    alen=min(chars(filename),10000)
  326.    stuff=charin(filename,1,alen)
  327. end
  328.  
  329. stuff=space(translate(stuff,' ','00090a0d1a1b'x))
  330. url_title=0
  331.  
  332. wow=look_header(filename)
  333.  
  334. if wow<>0 then do
  335.   do jmm=1 to words(srchwords)
  336.       aword=strip(word(srchwords,jmm))
  337.       wow=sref_make_block(aword,wow,'<b>','</b>')   /* highlight matches */
  338.   end
  339.   call lineout tempfile,'<dd> ' wow
  340.   return 0
  341. end
  342. if asummary<>3 then do           /* return if not CREATE */
  343.    call lineout tempfile,'<dd> <code> Explicit summary not available</code> ' 
  344.    return 0
  345. end
  346.  
  347. WOW=LOOK_HTAG()
  348. if wow<>0 then do
  349.  
  350.   do jmm=1 to words(srchwords)
  351.       aword=strip(word(srchwords,jmm))
  352.       wow=sref_make_block(aword,wow,'<b>','</b>')   /* highlight matches */
  353.   end
  354.   call lineout tempfile,'<dd> <code>'
  355.   call lineout tempfile, "  " wow
  356.   call lineout tempfile,'</code>'
  357.   return 0
  358. end
  359. if url_title<>0 then
  360.    call lineout tempfile,'<dd> <code> ' url_title ' </code> '
  361. else
  362.    call lineout tempfile,'<dd> <code> Summary not available </code> '
  363. return 0
  364.  
  365.  
  366.  
  367.  
  368. /* ----------------------------------------------------------------------- */
  369. /* Look for "desc" field in header     */
  370. /* ----------------------------------------------------------------------- */
  371.  
  372. look_header: procedure expose stuff url_title
  373. parse arg afile
  374. dowrite=0
  375. do until stuff=""
  376.  
  377.     parse var stuff  p1 '<' tag '>' stuff
  378.     if  translate(word(tag,1))="HEAD" then do   /* now in head !*/
  379.             dowrite=1
  380.             iterate
  381.     end
  382.     if dowrite=0 then iterate    /* wait till we get into head .. */
  383.  
  384.     if  translate(word(tag,1))="/HEAD" then  /* out of head, all done ! */
  385.         leave
  386.  
  387. /* IT IS A TITLE TAG?  */
  388.      if translate(word(tag,1))="TITLE" then do
  389.         parse var stuff url_title '<' footag '>' stuff
  390.      end
  391.  
  392. /* is it a  META HTTP-EQUIV or a META NAME ? */
  393.     if translate(word(tag,1))="META" then do
  394.         parse var tag ameta atype '=' rest
  395.         tatype=translate(atype)
  396.         if tatype="HTTP-EQUIV" | tatype="NAME" then do
  397.            parse var rest aval1 rest
  398.            REST=STRIP(REST)
  399.  
  400.            aval1=strip(aval1) ;
  401.            aval1=strip(aval1,,'"')
  402.            if abbrev(translate(aval1),'DESC')<>1 then iterate
  403.  
  404.            aval2=" "
  405.            foo1=ABBREV(translate(rest),'CONTENT')
  406.            if foo1>0 then do
  407.                 PARSE VAR REST FOO '=' AVAL2
  408.                 aval2=strip(aval2)
  409.                 aval2=strip(aval2,'b','"')
  410.                 WOW=LEFT(AVAL2,500)
  411.                 wow=sref_replacestrg(wow,'<','<','ALL')
  412.                 wow=sref_replacestrg(wow,'>','>','ALL')
  413.                 return WOW
  414.            end
  415.         end             /* name or http-equiv */
  416.     end         /* meta */
  417. end             /* stuff */
  418.  
  419. if stuff="" then say "Warning: </head> tag NOT found: " afile
  420.  
  421. return 0
  422.  
  423.  
  424. /* ----------------------------------------------------------------------- */
  425. /* Extract <hn> fields     */
  426. /* ----------------------------------------------------------------------- */
  427.  
  428. look_htag: procedure expose stuff filename
  429.  
  430. stuff0=left(stuff,1000)
  431.  
  432. amessage=""
  433. dowrite=0
  434. do until stuff=""
  435.     parse var stuff  p1 '<' tag '>' stuff
  436.     ttag=translate(word(tag,1))
  437.     if wordpos(ttag,' H1 H2 H3 H4 TITLE')>0 THEN DO   /* grab stuff */
  438.         parse var stuff  amess '<' tag2 '>' stuff
  439.         amessage=amessage||amess||'<b> | </b>'
  440.     end
  441. end
  442.  
  443. if amessage="" then do  /* getting desperate -- grab any old words! */
  444.    do until stuff0=""
  445.       parse var stuff0 p1 '<' tag '>' stuff0
  446.       amessage=amessage||' '||p1
  447.    end
  448. end
  449.  
  450. if amessage="" then
  451.    return 0
  452. amessage=left(amessage,300)  /* keep it short */
  453. return amessage
  454.  
  455.  
  456.  
  457.  
  458.